package model.entity.unit;

import model.crafting.PlayerTable;
import model.entity.building.Building;
import model.inventory.item.Item;
import model.world.World;
import model.world.tile.ResourceType;
import model.world.tile.Tile;

public class Player extends Unit {

	public Player(World world, Tile parent) {
		super(world, parent, 1);
		craftingTable = new PlayerTable(world.getRecipeComparer());
	}

	@Override
	public int getResourceCollectionRate(ResourceType t) {
		if(t == ResourceType.WOOD){
			if(inventory.findItem(50) == null)
				return 0;
			else
				return 1;
		}
		return 1;
	}

	public void stopWorking() {
		if(currentTile.getOccupyingEntity() == this){
			currentTile.removeOccupyingEntity(this);
			currentTile.addPresentUnit(this);
		}
	}
	
	public void placeItem(int itemID){
		if(currentTile.getOccupyingEntity() == null){
			Item placeable = this.getInventory().findItem(itemID);
			if(placeable.isPlaceable() && currentTile.getOccupyingEntity() == null){
				placeable = this.getInventory().remove(new Item(itemID, 1));
				currentTile.addOccupyingEntity(new Building(world, currentTile, placeable.getBuildingID()));
			}
		}
	}

	public void placeItem() {
		placeItem(getInventory().getSelectedItem().getID());
	}
}
